const app = getApp()
const db = wx.cloud.database()
const list = db.collection("IntangibleCulturalHeritage")
Page({
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    Custom: app.globalData.Custom,
    TabCur: 0,
    MainCur: 0,
    VerticalNavTop: 0,
    classifyList:[
      {
        index: 0,
        item: '民间文学',
        lists: [
          {
            title: '苗族古歌',
            desc: '苗族古歌，贵州省台江县、黄平县地方传统民间文学，国家级非物质文化遗产之一',
            avatar: 'https://bkimg.cdn.bcebos.com/pic/b64543a98226cffc6f90096fb5014a90f703eab1?x-bce-process=image/watermark,image_d2F0ZXIvYmFpa2U4MA==,g_7,xp_5,yp_5/format,f_auto'
          },
          {
            title: '布洛陀',
            desc: '布洛陀，中国广西百色敢壮山地方传统民间文学，国家级非物质文化遗产之一。',
            avatar: 'https://bkimg.cdn.bcebos.com/pic/80cb39dbb6fd5266a7595585a218972bd4073634?x-bce-process=image/watermark,image_d2F0ZXIvYmFpa2UxNTA=,g_7,xp_5,yp_5/format,f_auto'
          },
          {
            title: '遮帕麻和遮咪麻',
            desc: '遮帕麻和遮咪麻，云南省梁河县地方传统民间文学，国家级非物质文化遗产之一。',
            avatar: 'http://5b0988e595225.cdn.sohucs.com/images/20180821/57ffbae1f6f7436398d8154c959abb10.jpg'
          },
          {
            title: '白蛇传传说',
            desc: '白蛇传传说，江苏省镇江市、浙江省杭州市地方传统民间文学，国家级非物质文化遗产之一。',
            avatar: 'https://bkimg.cdn.bcebos.com/pic/bd315c6034a85edfa982a76b44540923dd54756f?x-bce-process=image/watermark,image_d2F0ZXIvYmFpa2U5Mg==,g_7,xp_5,yp_5/format,f_auto'
          },
        ]
      },
      {
        index: 1,
        item: '传统音乐',
        lists: []
      },
      {
        index: 2,
        item: '民间舞蹈',
        lists: []
      },
      {
        index: 3,
        item: '传统戏剧',
        lists: []
      },
      {
        index: '4',
        item: '曲艺',
        lists: []
      },
      {
        index: '5',
        item: '民间美术',
        lists: []
      },
      {
        index: '6',
        item: '传统技艺',
        lists: []
      }
    ],
    list: [],
    load: true,
    showList: [
      {
        index: 0,
        url: 'https://hellorfimg.zcool.cn/provider_image/preview260/2235741974.jpg'
      },
      {
        index: 1,
        url: 'https://hellorfimg.zcool.cn/preview260/11369068.jpg'
      },
      {
        index: 2,
        url: 'https://hellorfimg.zcool.cn/provider_image/preview260/2237566781.jpg'
      },
      {
        index: 3,
        url: 'https://hellorfimg.zcool.cn/provider_image/preview260/2238532505.jpg'
      }
    ]
  },
  toDescPage: function(e)
  {
    const title = e.currentTarget.dataset.title
    const item = e.currentTarget.dataset.type
    console.log(title)
    console.log(item)
    wx.navigateTo({
      url: '/pages/show/show?title=' + title + "&type=" + item
    })
  },
  onLoad() {
    wx.showLoading({
      title: '加载中...',
      mask: true
    });
    let list = [{}];
    for (let i = 0; i < 26; i++) {
      list[i] = {};
      list[i].name = String.fromCharCode(65 + i);
      list[i].id = i;
    }
    this.setData({
      list: list,
      listCur: list[0]
    })
    wx.cloud.callFunction({
      name: "getCultureItem",
      data: {
        index: 0
      }
    }).then(res => {
      console.log(res)
      let key = `classifyList[0].lists`
      this.setData({
        [key]: res.result.data
      })
    })
  },
  onReady() {
    wx.hideLoading()
  },
  tabSelect(e) {
    this.setData({
      TabCur: e.currentTarget.dataset.id,
      MainCur: e.currentTarget.dataset.id,
      VerticalNavTop: (e.currentTarget.dataset.id - 1) * 50
    })
    wx.cloud.callFunction({
      name: "getCultureItem",
      data: {
        index: e.currentTarget.dataset.id
      }
    }).then(res => {
      console.log(res)
      let key = `classifyList[`+ e.currentTarget.dataset.id +`].lists`
      this.setData({
        [key]: res.result.data
      })
    })
  },
  VerticalMain(e) {
    let that = this;
    let list = this.data.list;
    let tabHeight = 0;
    if (this.data.load) {
      for (let i = 0; i < list.length; i++) {
        let view = wx.createSelectorQuery().select("#main-" + list[i].id);
        view.fields({
          size: true
        }, data => {
          list[i].top = tabHeight;
          tabHeight = tabHeight + data.height;
          list[i].bottom = tabHeight;     
        }).exec();
      }
      that.setData({
        load: false,
        list: list
      })
    }
    let scrollTop = e.detail.scrollTop + 20;
    for (let i = 0; i < list.length; i++) {
      if (scrollTop > list[i].top && scrollTop < list[i].bottom) {
        that.setData({
          VerticalNavTop: (list[i].id - 1) * 50,
          TabCur: list[i].id
        })
        return false
      }
    }
  }
})